package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdWildReport;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdWildReportDaoImpl extends TempDataObjectDao<StrongholdWildReport> {
    public static String getStrongholdWildReportSql = "select * from t_u_activity_stronghold_wild_report where activityId=? and userId=? order by reportTime desc ";
    public Map<Integer, List<StrongholdWildReport>> getStrongholdWildReport(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<StrongholdWildReport>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdWildReportSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++,userId);
            rs = call.executeQuery();
            List<StrongholdWildReport> list = new ArrayList<>();
            while (rs.next()) {
                StrongholdWildReport strongholdWildReport = parse(rs);
                list.add(strongholdWildReport);
            }
            dataMap.put(activityId, list);
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdWildReportSql = "update t_u_activity_stronghold_wild_report set enemyServerId=?,enemyUserBaseInfo=?,rewardPoints=?,reportTime=?,win=?,boxList = ?,isRead = ? where activityId=? and userId=? and reportTime = ?" ;
    @Override
    protected boolean updateData(StrongholdWildReport strongholdWildReport) {
        String sql = "update t_u_activity_stronghold_wild_report set isRead = ? where activityId=? and userId=? and reportTime = ?";
        strongholdWildReport.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWildReport.isRead()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildReport.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildReport.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildReport.getReportTime()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdWildReportSql = "insert into t_u_activity_stronghold_wild_report(activityId,userId,enemyServerId,enemyUserBaseInfo,rewardPoints,reportTime,win,type,boxList,enemyUserId,isRead) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(StrongholdWildReport strongholdWildReport) {
        strongholdWildReport.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildReport.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildReport.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildReport.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.NVARCHAR,JSON.toJSONString(strongholdWildReport.getEnemyUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildReport.getRewardPoints()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildReport.getReportTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWildReport.isWin()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildReport.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdWildReport.getBoxList())));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildReport.getEnemyUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWildReport.isRead()));
        if (getDb().executeNoneQuery(insertStrongholdWildReportSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdWildReport parseData(ResultSet rs) throws SQLException {StrongholdWildReport strongholdWildReport = new StrongholdWildReport();
        strongholdWildReport.setActivityId(rs.getInt("activityId"));
        strongholdWildReport.setUserId(rs.getLong("userId"));
        strongholdWildReport.setEnemyServerId(rs.getLong("enemyServerId"));
        strongholdWildReport.setEnemyUserBaseInfo(JSON.parseObject(rs.getString("enemyUserBaseInfo"),UserBaseInfo.class));
        strongholdWildReport.setRewardPoints(rs.getInt("rewardPoints"));
        strongholdWildReport.setReportTime(rs.getLong("reportTime"));
        strongholdWildReport.setWin(rs.getBoolean("win"));
        strongholdWildReport.setType(rs.getInt("type"));
        strongholdWildReport.setEnemyUserId(rs.getLong("enemyUserId"));
        strongholdWildReport.setBoxList(JSON.parseArray(rs.getString("boxList"),Integer.class));
        strongholdWildReport.setRead(rs.getBoolean("isRead"));

        return strongholdWildReport;
    }

}
